package com.zhoug.mpchart.data;

import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.zhoug.mpchart.core.MPConstant;
import com.zhoug.mpchart.core.MPEntryList;
import com.zhoug.mpchart.core.MPLineKey;
import com.zhoug.mpchart.core.styleconfig.IMPLineStyleConfig;
import com.zhoug.mpchart.core.styleconfig.MPChartStyleDefaultUtils;
import com.zhoug.mpchart.core.styleconfig.MPLineStyleConfig;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;

/**
 * 曲线图 数据工厂
 *
 * @Author: zhoug
 * @Date: 2024-02-03
 * @Description:
 */
public class MPLineChartDataCreator {
    private IMPLineStyleConfig styleConfig;
    private List<String> mXLabels = new ArrayList<>();
    private OnDateSetCreateListener<LineDataSet> mOnDateSetCreateListener;
    private ValueFormatter mDataSetValueFormatter;

    public MPLineChartDataCreator() {
        this(new MPLineStyleConfig());
    }

    public MPLineChartDataCreator(IMPLineStyleConfig styleConfig) {
        if (styleConfig != null) {
            this.styleConfig = styleConfig;
        } else {
            this.styleConfig = new MPLineStyleConfig();
        }
    }

    public void setStyleConfig(@NonNull IMPLineStyleConfig styleConfig) {
        this.styleConfig = styleConfig;
    }

    public void setDataSetValueFormatter(ValueFormatter dataSetValueFormatter) {
        this.mDataSetValueFormatter = dataSetValueFormatter;
    }

    public void setOnDateSetCreateListener(OnDateSetCreateListener<LineDataSet> onDateSetCreateListener) {
        this.mOnDateSetCreateListener = onDateSetCreateListener;
    }

    public List<String> getXLabels() {
        return mXLabels;
    }


    public LineData crateLineData(List<HkChartData> list, List<MPLineKey> mpLineKeys) {
        //x轴名称
        if (mXLabels == null) {
            mXLabels = new ArrayList<>();
        } else {
            mXLabels.clear();
        }
        if (list != null && list.size() > 0) {
            if (mpLineKeys == null || mpLineKeys.size() == 0) {
                mpLineKeys = createMPLineKey(list);
            }
            if (mpLineKeys == null || mpLineKeys.size() == 0) {
                return null;
            }
            List<MPEntryList<Entry>> lineEntries = new ArrayList<>();   //曲线图
            for (MPLineKey mplineKey : mpLineKeys) {
                lineEntries.add(new MPEntryList<>(mplineKey.getKeys(), mplineKey.getAxisDependency()));
            }
            for (int i = 0; i < list.size(); i++) {
                int x = i + 1;
                HkChartData hkChartData = list.get(i);
                String name = hkChartData.getName();
                mXLabels.add(name);
                initMPEntryList(lineEntries, x, hkChartData);
            }
            return createLineData(lineEntries);
        }
        return null;
    }

    /**
     * 根据配置创建默认的 List<MPBarLineKey>
     *
     * @param list
     * @return
     */
    protected List<MPLineKey> createMPLineKey(List<HkChartData> list) {
        String[] keys = HkChartData.getKeys(list);
        if (keys != null && keys.length > 0) {
            List<MPLineKey> result = new ArrayList<>();
            for (String key : keys) {
                result.add(new MPLineKey(key));
            }
            return result;
        }
        return null;
    }

    /**
     * 使用 HkChartData 初始化MPEntryList中的entries
     * @param lineEntries
     * @param x
     * @param hkChartData
     */
    public static void initMPEntryList(List<MPEntryList<Entry>> lineEntries, float x, HkChartData hkChartData) {
        if (lineEntries == null || lineEntries.size() == 0 || hkChartData==null) return;
        //曲线图
        for (MPEntryList<Entry> item : lineEntries) {
            String[] keys = item.getKeys();
            item.addEntry(new Entry(x, hkChartData.getValue(keys[0]), hkChartData));
        }
    }

    /**创建LineData
     *
     * @param lineEntries
     * @return
     */
    public  LineData createLineData(List<MPEntryList<Entry>> lineEntries) {
        if (lineEntries == null || lineEntries.size() == 0) return null;
        //曲线图
        List<ILineDataSet> dataSets = new ArrayList<>();
        for (int i = 0; i < lineEntries.size(); i++) {
            MPEntryList<Entry> mpEntryList = lineEntries.get(i);
            LineDataSet lineDataSet = new LineDataSet(mpEntryList.getEntries(), mpEntryList.getKeys()[0]);
            if (mpEntryList.getAxisDependency() == MPConstant.RIGHT) {
                lineDataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
            } else {
                lineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
            }
            int lineColor = styleConfig.getLineColor(i);
            lineDataSet.setColor(lineColor);
            lineDataSet.setValueFormatter(mDataSetValueFormatter);
            MPChartStyleDefaultUtils.initLineDataSet(lineDataSet);
            if (mOnDateSetCreateListener != null) {
                mOnDateSetCreateListener.onInitDataSetStyle(lineDataSet, i);
            }
            dataSets.add(lineDataSet);
        }
        return new LineData(dataSets);
    }

}
